<template>
  <div v-if="!(hidden && totalPage < 1)" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="current"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizeOptions"
      :total="total"
      v-bind="$attrs"
      :pager-count="5"
      :hide-on-single-page="hidden"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { scrollTo } from "@/libs/scroll-to";
export default {
  name: "ElemePagination",
  props: {
    //数据总数
    total: {
      type: Number,
      required: true,
      default: 0,
      validator: function (value) {
        return value >= 0;
      },
    },
    //当前页数
    page: {
      type: Number,
      default: 1,
      validator: function (value) {
        return value > 0;
      },
    },
    //每页条数
    limit: {
      type: Number,
      default() {
        return this.$Conf.pageSize || 10;
      },
    },
    //指定每页可以显示多少条
    pageSizeOptions: {
      type: Array,
      default() {
        return [10, 20, 30, 50, 70, 80, 100];
      },
    },
    //布局
    layout: {
      type: String,
      default: "total, prev, pager, next, jumper", //sizes,
    },
    //是否为分页按钮添加背景色
    background: { type: Boolean, default: true },
    //是否自动滚动到表格头部
    autoScroll: { type: Boolean, default: true },
    //只有一页时是否隐藏
    hidden: { type: Boolean, default: true },
  },
  computed: {
    current: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      },
    },
    pageSize: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit("update:limit", val);
      },
    },
    totalPage: function () {
      return Math.ceil(this.total / this.pageSize);
    },
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("change", { page: this.current, limit: val });
      this.autoScroll && scrollTo(0, 800);
    },
    handleCurrentChange(val) {
      this.$emit("change", { page: val, limit: this.pageSize });
      this.autoScroll && scrollTo(0, 800);
    },
  },
};
</script>

<style lang="scss" scoped>
.pagination-container {
  margin-top: 30px;

  ::v-deep .el-pagination {
    .el-input__inner {
      padding: 0;
    }
    .el-pagination__jump {
      margin-left: 10px;
    }

    .el-input__suffix {
      display: none;
    }
  }
}
</style>